package com.hmall.vo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class PageVo<T> {
	//最好给默认值
	static final    Integer MIN=1;
	static final    Integer MAX=8;
	private Integer pageNo;//当前页
	private Integer pageSize;//每个页有多少行数据
	private Integer totalRecord;//总数量
	private  List<T> data;//数据
	private Integer startIndex;
	private Integer totalPages;//共多少页
	private Integer start;//分页显示的页数
	private Integer end;
	private boolean hasNextPage;
	private List<Integer> indexList=new ArrayList<Integer>();
	public PageVo(){

	}
	public PageVo(Integer pageNo, Integer pageSize, Integer totalRecord,List<T> data) {
		this.pageNo = pageNo;
		this.pageSize = pageSize;
		this.totalRecord = totalRecord;
        //说明页码和总数可以整除
        if (totalRecord%pageSize==0) {
            totalPages=totalRecord/pageSize;
        }else {
            totalPages=totalRecord/pageSize+1;
        }
		this.data=data;
        if(pageNo>=totalPages) {
            hasNextPage = false;
			pageNo=totalPages;
			return;
        } else{
            hasNextPage = true;
        }


		start=MIN;
		end=MAX;
		if (totalPages<=MAX) {//如果总页数小于5 end=总页数
			end=totalPages;
		}else {
			start=pageNo-4;//页码改变
			end=pageNo+4;
			if (start<=0) {//如果开始小于0 恢复初始
				start=MIN;
				end=MAX;
			}if (end>totalPages) {//如果end大于总页码
				end=totalPages;//赋值开始和截止
				start=totalPages-MAX;
			}
		}
		//将页码存入页码集合中[3,4,5]
		for (int i = start; i <= end; i++) {
			indexList.add(i);
		}
	}

	public Integer getPageNo() {
		return pageNo;
	}

	public void setPageNo(Integer pageNo) {
		this.pageNo = pageNo;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public Integer getTotalRecord() {
		return totalRecord;
	}

	public void setTotalRecord(Integer totalRecord) {
		this.totalRecord = totalRecord;
	}

	public Integer getStartIndex() {
		return startIndex;
	}

	public void setStartIndex(Integer startIndex) {
		this.startIndex = startIndex;
	}

	public Integer getTotalPages() {
		return totalPages;
	}

	public void setTotalPages(Integer totalPages) {
		this.totalPages = totalPages;
	}

	public static Integer getMIN() {
		return MIN;
	}

	public static Integer getMAX() {
		return MAX;
	}



	public Integer getStart() {
		return start;
	}
	public void setStart(Integer start) {
		this.start = start;
	}
	public Integer getEnd() {
		return end;
	}
	public void setEnd(Integer end) {
		this.end = end;
	}
	public List<Integer> getIndexList() {
		return indexList;
	}
	public void setIndexList(List<Integer> indexList) {
		this.indexList = indexList;
	}
	public boolean isHasNextPage() {
		return hasNextPage;
	}
	public void setHasNextPage(boolean hasNextPage) {
		this.hasNextPage = hasNextPage;
	}

	public List<T> getData() {
		return data;
	}

	public void setData(List<T> data) {
		this.data = data;
	}

	@Override
	public String toString() {
		return "PageVo{" +
				"pageNo=" + pageNo +
				", pageSize=" + pageSize +
				", totalRecord=" + totalRecord +
				", data=" + data +
				", startIndex=" + startIndex +
				", totalPages=" + totalPages +
				", start=" + start +
				", end=" + end +
				", hasNextPage=" + hasNextPage +
				", indexList=" + indexList +
				'}';
	}
}
